package com.yjb.demo.websocket.server.application;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;

/**
 * @author yjb
 * @description
 * @create 2024/9/23 19:32
 */
@Slf4j
@Component
public class SendTests {

    @PostConstruct
    public void sendTest() throws InterruptedException {
        new Thread(()->{
            while (true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                WebSocketConstant.sessionList.forEach(session -> {
                    try {
                        if (session.isOpen()) {
                            session.getBasicRemote().sendText("hello");
                        }
                    } catch (IOException e) {
                        log.error("发送到用户信息失败, Error: {}", e.getMessage());
                    }
                });
            }
        }).start();

    }
}
